package com.zjeav.lingjiao.ui.home;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Toast;

import com.jpeng.jptabbar.BadgeDismissListener;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.baseBean.Adapter;
import com.zjeav.lingjiao.base.baseBean.BaseActivity;
import com.zjeav.lingjiao.base.baseBean.BaseApplication;
import com.zjeav.lingjiao.base.tools.LocalUtils;
import com.zjeav.lingjiao.base.tools.SharedPreferencesUtils;
import com.zjeav.lingjiao.base.tools.SystemTools;
import com.zjeav.lingjiao.ui.music.MusicActivity;
import com.zjeav.lingjiao.ui.music.MusicService;

import org.xutils.DbManager;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.zjeav.lingjiao.base.tools.SystemTools.isServiceRunning;
import static com.zjeav.lingjiao.ui.music.MusicActivity.MUSIC_DURATION;
import static com.zjeav.lingjiao.ui.music.MusicActivity.PLAY_STATUE;
import static com.zjeav.lingjiao.ui.music.MusicService.MUSIC_CURRENT;
import static com.zjeav.lingjiao.ui.music.MusicService.UPDATE_ACTION;

/**
 * 主界面
 */

public class RestructureMainActivity extends BaseActivity implements BadgeDismissListener, OnTabSelectListener {

    @Titles
    private static final int[] mTitles = {R.string.home, R.string.book_market, R.string.find, R.string.mine};

    @SeleIcons
    private static final int[] mSeleIcons = {R.mipmap.tabbar_homepage_sel, R.mipmap.tabbar_bookstore_sel, R.mipmap.tabbar_discover_sel, R.mipmap.tabbar_user_sel};

    @NorIcons
    private static final int[] mNormalIcons = {R.mipmap.tabbar_homepage_normal, R.mipmap.tabbar_bookstore_normal, R.mipmap.tabbar_discover_normal, R.mipmap.tabbar_user_normal};

    private List<Fragment> list = new ArrayList<>();

    private ViewPager mPager;

    private JPTabBar mTabbar;

    private HomeFragment mTab1;

    private BookMarkFragment mTab2;

    private FindFragment mTab3;

    private MineFragment mTab4;
    private DbManager db;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restructure_layout);
        initView();
        initData();


    }

    private void initView() {
        mTabbar = findViewById(R.id.tabbar);
        mPager = findViewById(R.id.view_pager);
        db = BaseApplication.dbManager;
//        mTabbar.setTitles("asd","页面二","页面三","页面四").setNormalIcons(R.mipmap.tab1_normal,R.mipmap.tab2_normal,R.mipmap.tab3_normal,R.mipmap.tab4_normal)
//                .setSelectedIcons(R.mipmap.tab1_selected,R.mipmap.tab2_selected,R.mipmap.tab3_selected,R.mipmap.tab4_selected).generate();
        mTabbar.setTabTypeFace("fonts/Jaden.ttf");
        mTab1 = new HomeFragment();
        mTab2 = new BookMarkFragment();
        mTab3 = new FindFragment();
        mTab4 = new MineFragment();
        mTabbar.setGradientEnable(true);
        mTabbar.setPageAnimateEnable(true);
        mTabbar.setTabListener(this);
        list.add(mTab1);
        list.add(mTab2);
        list.add(mTab3);
        list.add(mTab4);
    }

    private void initData() {
        //广播
        MusicPlayerReceiver musicPlayerReceiver;
        //注册广播
        musicPlayerReceiver = new MusicPlayerReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(UPDATE_ACTION);
        filter.addAction(MUSIC_CURRENT);
        filter.addAction(MUSIC_DURATION);
        filter.addAction(PLAY_STATUE);

        registerReceiver(musicPlayerReceiver, filter);

        mPager.setAdapter(new Adapter(getSupportFragmentManager(), list));
        mTabbar.setContainer(mPager);
        //设置Badge消失的代理
        mTabbar.setDismissListener(this);
        mTabbar.setTabListener(this);
        if (mTabbar.getMiddleView() != null)
            mTabbar.getMiddleView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    ImageView imageView=new ImageView(getApplicationContext());
//                    imageView.setImageResource(R.mipmap.bookcover_1);

//                    (ImageView) imageView.setImageResource();

                    int bookid = (int) SharedPreferencesUtils.getParam(getApplicationContext(), "bookid", -1);
                    int position=(int)SharedPreferencesUtils.getParam(getApplicationContext(),"position",0);
                    int currentTime=(int) SharedPreferencesUtils.getParam(getApplicationContext(),"currentTime",0);
                    if (currentTime!=0){
                        ((CircleImageView) (mTabbar.getMiddleView())).setImageResource(R.mipmap.bookcover_1);
                        Animation animation = AnimationUtils.loadAnimation(RestructureMainActivity.this, R.anim.img_animation);
                        LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
                        animation.setInterpolator(lin);
                        mTabbar.getMiddleView().startAnimation(animation);
                        MusicActivity.mp3Infos = LocalUtils.getList(db, bookid);
                        if (MusicActivity.mp3Infos == null) {
                            Toast.makeText(RestructureMainActivity.this, "暂无下载", Toast.LENGTH_SHORT).show();
                        } else {
                            if (isServiceRunning(getBaseContext(), "com.flashmusic.MusicService")) {
                                stopService(new Intent(RestructureMainActivity.this, MusicService.class));
                            }else {
                                SharedPreferencesUtils.setParam(RestructureMainActivity.this,"currentTime",0);
                            }
                            Intent musicintent = new Intent(RestructureMainActivity.this, MusicActivity.class);
                            //当前播放的位置
                            musicintent.putExtra("position", String.valueOf(position));
                            musicintent.putExtra("currentTime", currentTime);
                            musicintent.putExtra("type", 0);
                            //     MusicActivity.musicserviceintent = intent;
                            //启动音乐播放界面
                            startActivity(musicintent);
                        }
                    }

//                    Toast.makeText(RestructureMainActivity.this,"中间点击",Toast.LENGTH_SHORT).show();;
                }
            });
    }

    public class MusicPlayerReceiver extends BroadcastReceiver {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //当前时间更新
            if (action.equals(PLAY_STATUE)) {
                boolean playstatue = intent.getBooleanExtra("playstatue", true);
                if (playstatue) {
                    ((CircleImageView) (mTabbar.getMiddleView())).setImageResource(R.mipmap.bookcover_1);
                    Animation animation = AnimationUtils.loadAnimation(RestructureMainActivity.this, R.anim.img_animation);
                    LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
                    animation.setInterpolator(lin);
                    mTabbar.getMiddleView().startAnimation(animation);
//                    MusicPlay.setImageDrawable(getResources().getDrawable(R.drawable.musicplay, null));
//                    isPlaying = true;
                } else {
                    mTabbar.getMiddleView().clearAnimation();
                    ((CircleImageView) (mTabbar.getMiddleView())).setImageResource(R.mipmap.tabbar_play_normal);
//                    MusicPlay.setImageDrawable(getResources().getDrawable(R.drawable.musicpause, null));
//                    isPlaying = false;
                }
            }

        }
    }

    @Override
    public void onDismiss(int position) {

    }

    @Override
    public void onTabSelect(int index) {

    }

    @Override
    public boolean onInterruptSelect(int index) {
        return false;
    }

    public JPTabBar getTabbar() {
        return mTabbar;
    }
}
